/*
 * NetworkSubSystem.cpp
 *
 *  Created on: Apr 16, 2017
 *      Author: zhangjf
 */

#include <Poco/Net/TCPServer.h>
#include "Poco/Util/Application.h"
#include <Poco/FileChannel.h>
#include <Poco/DateTimeFormatter.h>
#include <Poco/PatternFormatter.h>
#include <Poco/FormattingChannel.h>

using namespace Poco;
using namespace Poco::Net;
using namespace Poco::Util;

#include "NCSSubsystem.h"
#include "NetworkSubsystem.h"
#include "Helper.h"

using namespace util;

namespace network
{

const std::string NetworkSubsystem::_name = "NetworkSubsystem";
NetworkSubsystem* NetworkSubsystem::_pInstance = nullptr;

NetworkSubsystem::NetworkSubsystem()
: NCSSubsystem(name())
,_nq()
, _nc()
, _tcpServer(_nq)
, _packetDispatcher(_nq)
//, _logger(Application::instance().logger().get(_name))
{
	// TODO Auto-generated constructor stub
	if (_pInstance != nullptr)
		return;
	_pInstance = this;

	poco_information(logger(), __func__ + string(": ") + "ok");
}

NetworkSubsystem::~NetworkSubsystem()
{
	// TODO Auto-generated destructor stub
}

void NetworkSubsystem::initialize(Application& self)
{
	// TODO initialize
	//initLogger(fname);
	_tcpServer.initialize();
	poco_information(logger(), __func__ + string(": ") + "ok");
}

void NetworkSubsystem::uninitialize()
{
	// TODO initialize
}

//void NetworkSubsystem::initLogger(const std::string& strLogName)
//{
//	AutoPtr<FileChannel> pChannel(new FileChannel);
//	pChannel->setProperty("path", strLogName);
//	pChannel->setProperty("rotation", " 20 M");
//	pChannel->setProperty("archive", "timestamp");
//
//	AutoPtr<PatternFormatter> pPF(new PatternFormatter);
//	pPF->setProperty("pattern", "%Y-%m-%d %H:%M:%S %s(%T): %t");
//	pPF->setProperty("times","local");
//	AutoPtr<FormattingChannel> pFC(new FormattingChannel(pPF, pChannel));
//	_logger.setChannel(pFC);
//}

void NetworkSubsystem::start()
{
	ThreadPool::defaultPool().start(_packetDispatcher);
	ThreadPool::defaultPool().start(_tcpServer);
}

void NetworkSubsystem::stop()
{
	_tcpServer.stop();
	_packetDispatcher.cancel();

}

} /* namespace network */

